unit updatetest;

interface

uses Windows, Classes, SqlExpr, TestBase;

type

  TUpdateTest = class(TAbstractTest)
  private
    FSQL: TStrings;
    FAttempts: Integer;
    function RunFor(Connection: TSQLConnection): DWORD;
  protected
    function GetName: string; override;
    function GetDescription: string; override;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Configure; override;
    procedure Run; override;
    property SQL: TStrings read FSQL;
    property Attempts: Integer read FAttempts write FAttempts;
  end;

implementation

uses SysUtils, DB, DBXpress, Forms, Data;

{ TUpdateTest }

constructor TUpdateTest.Create;
var
  FileName: string;
begin
  inherited;
  FSQL := TStringList.Create;
  FileName := ExtractFilePath(Application.ExeName) + '\update.sql';
  if FileExists(FileName) then
    FSQL.LoadFromFile(FileName);
  FAttempts := IniFile.ReadInteger('update test', 'loopcount', 100000);
end;

destructor TUpdateTest.Destroy;
begin
  FSQL.SaveToFile(ExtractFilePath(Application.ExeName) + '\select.sql');
  FSQL.Free;
  IniFile.WriteInteger('update test', 'loopcount', FAttempts);
  inherited;
end;

procedure TUpdateTest.Configure;
begin
  inherited;

end;

function TUpdateTest.GetName: string;
begin
  Result := 'Update database test';
end;

function TUpdateTest.GetDescription: string;
begin
  Result := 'Test executes series of update staments.';
end;

procedure TUpdateTest.Run;
var
  ResultOld, ResultNew: Integer;
begin
  if DataMain.connectionOld.Tag = 1 then
  begin
    DataMain.connectionOld.Tag := 0;
    ResultOld := RunFor(DataMain.connectionOld);
    ResultNew := RunFor(DataMain.connectionNew);
  end else
  begin
    DataMain.connectionOld.Tag := 1;
    ResultNew := RunFor(DataMain.connectionNew);
    ResultOld := RunFor(DataMain.connectionOld);
  end;
  OnReceiveResults(ResultOld, ResultNew);
end;

function TUpdateTest.RunFor(Connection: TSQLConnection): DWORD;
var
  I: Integer;
  Query: TSQLQuery;
begin
  Query := TSQLQuery.Create(nil);
  try
    Query.SQLConnection := Connection;
    Connection.Open;
    Query.SQL.Assign(FSQL);
    //Query.Params[0].AsString := DateTimeToStr(Now);
    //Query.Params[0].AsFloat := Now;
    Query.Params[0].DataType := ftBlob;
    Query.Params[0].Value := FSQL.Text;
    Query.ExecSQL;
    Result := GetTickCount;
    for I := 0 to FAttempts - 1 do
      Query.ExecSQL;
    Result := GetTickCount - Result;
  finally
    Query.Free;
  end;
end;

initialization
  TestList.Add(TUpdateTest.Create);

end.
